import 'whatwg-fetch'
import axios from 'axios'
import { message, Alert } from 'antd';
import session from '../utils/Session'

message.config({
    top: 150,
    duration: 2,
})


function postData(url, data) {
    // Default options are marked with *
    /* if (url && url.includes('/center/') && process.env.Text[0] == 'https://market.mshiedu.com') {
        url = "https://center.mshiedu.com" + url;
    } else if (url && !url.includes('http')) {
        url = process.env.Text[0] + url
    } */
    if(!url.includes('http') && !url.includes('https')){
        url = process.env.Text[0] + url
    }
    let version = process.env.Text[5]
    // if(url.indexOf('/center/')>0){//center接口
    //     version = session.centerVersion
    // }else{
    //     version = session.version;
    // }
    
    return fetch(url, {
        body: JSON.stringify(data), // must match 'Content-Type' header
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, same-origin, *omit
        headers: {
            'user-agent': 'Mozilla/4.0 MDN Example',
            'content-type': 'application/json',
            "authorization":sessionStorage.getItem('token')?sessionStorage.getItem('token'):"",
            "version":version
        },
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, *same-origin
        redirect: 'follow', // manual, *follow, error
        referrer: 'no-referrer', // *client, no-referrer
    })
        .then(res => {
            if (res.ok) {
                return res.json();
            } else {
                throw `${res.status}, ${res.statusText}`;
            }
        })
        .then(data => {
            // if (response.status === 200 && (data.status === 0 || data.status === '0' || data.status === 10 || data.code === 200 || !data.status)) {
            if (data.status === 0 || data.status === '0' || data.status === 10 || data.code === 200 || !data.status) {
                return data.data;
            } else if (data.status === 1 && data.code == 'SEN001') {
                //token失效，跳转到登录页面，清除用户信息
                window.location.href = `${window.location.origin}/#/login`;

                throw data.msg
            } else if (data.code == "REQ007") {
                message.error(data.msg);
                return false;
            } else if (data.code === 'REQ10001'){//非法请求
                // 判断线上域名
                let hostname = window.location.hostname
                let url = process.env.Text[2]
                if(hostname === 'sys.myunedu.com'){//慧简学机构
                    url = window.location.origin
                }
                window.location.href = url
            } else {
                throw data.msg
            }
            // return data;
        })
        .catch(err => {
        if(err.substr(0,3) == 429){  //添加一层status = 429判断
            if (!window.isNotShowTooFastTips) {
              window.isNotShowTooFastTips = true
              message.error('用户点击过快，请稍后再试~~')
              if(window.timerTooFast) {
                window.clearTimeout(window.timerTooFast)
                window.timerTooFast = null
              }
              window.timerTooFast = window.setTimeout(function () {
                window.isNotShowTooFastTips = false
              }, 3000)
            }
            }else{
                message.error(err.msg)
            }
            // message.error(err);
        });
}


//用来导出表格
function exportData(url, data) {
    if (url && !url.includes('http')) {
        url = process.env.Text[0] + url
    }
    axios.defaults.headers['authorization'] = sessionStorage.getItem('token');
    axios.defaults.headers['version'] = process.env.Text[5];
    return axios({
        method: 'post',
        url: url,
        data: data,
        responseType: 'blob'
    }).then((res) => {
        return (res.data);
    }).catch((err) => {
        message.error(err.msg)
    })
}

export {
    postData,
    exportData,
};
